namespace cnVery.bbs.Entity
{
    using cnVery.Utils;
    using System;
    using System.Text.RegularExpressions;
    using System.Web;
    using System.Web.Caching;

    public class Skin
    {
        private bool _skinCookieRewrited;
        private string[] _skinModules;
        private string _skinName;

        public Skin()
        {
            this.ReadCurrentSkinName();
            this.ReadCurrentSkinModules();
        }

        private bool FindVersionKeyword(string str)
        {
            string[] strArray = new string[] { @"<!\-\-.+\-\->", @"<.*style\s*=.*display\s*:\s*none.*>[^<]*\$bbsVersion", @"<.*style\s*=.*visibility\s*:\s*hidden.*>[^<]*\$bbsVersion", "<[^>]*>" };
            string input = str;
            for (int i = 0; i < strArray.Length; i++)
            {
                input = Regex.Replace(input, strArray[i], "", RegexOptions.Multiline | RegexOptions.IgnoreCase);
            }
            return (input.IndexOf("$bbsVersion") != -1);
        }

        private void ReadCurrentSkinModules()
        {
            bool flag = bool.Parse(Config.Settings["EnableSkinCache"]);
            object obj2 = Caching.Get("Skin_" + this._skinName);
            if ((obj2 != null) && flag)
            {
                this._skinModules = (string[]) obj2;
            }
            else
            {
                string str = "skin/" + this._skinName + "/module/";
                this._skinModules = new string[4];
                string[] filenames = new string[] { Fetch.MapPath(str + "page_header.txt"), Fetch.MapPath(str + "page_footer.txt"), Fetch.MapPath(str + "table_header.txt"), Fetch.MapPath(str + "table_footer.txt") };
                for (int i = 0; i < filenames.Length; i++)
                {
                    this._skinModules[i] = TxtReader.Read(filenames[i]);
                }
                if (flag)
                {
                    Caching.Set("Skin_" + this._skinName, this._skinModules, new CacheDependency(filenames));
                }
            }
        }

        private void ReadCurrentSkinName()
        {
            if (Fetch.Get("skin") != "")
            {
                this._skinName = Fetch.Get("skin");
                this.UpdateSkinCookie();
            }
            else
            {
                HttpCookie cookie = Cookie.Get("skin");
                if (cookie == null)
                {
                    this._skinName = Config.Settings["DefaultSkin"];
                    this.UpdateSkinCookie();
                }
                else
                {
                    this._skinName = cookie.Value;
                }
            }
        }

        private void UpdateSkinCookie()
        {
            HttpCookie cookie = Cookie.Set("skin");
            cookie.Value = this._skinName;
            cookie.Expires = DateTime.Now.AddYears(1);
            Cookie.Save(cookie);
            this._skinCookieRewrited = true;
        }

        public string PageFooterModule
        {
            get
            {
                return this._skinModules[1];
            }
        }

        public string PageHeaderModule
        {
            get
            {
                return this._skinModules[0];
            }
        }

        public bool SkinCookieRewrited
        {
            get
            {
                return this._skinCookieRewrited;
            }
        }

        public string SkinName
        {
            get
            {
                return this._skinName;
            }
        }

        public string TableFooterModule
        {
            get
            {
                return this._skinModules[3];
            }
        }

        public string TableHeaderModule
        {
            get
            {
                return this._skinModules[2];
            }
        }
    }
}

